// --------sys_dict_data  获取字典配置详情

function getPlaceholders(condition) {
	const columnsArr = Object.keys(condition);
	const placeholders = [];
	const tempParams = [];
	columnsArr.forEach((item) => {
		if (item == 'dictLabel') {
			placeholders.push(`dict_label LIKE ?`);
			tempParams.push('%' + condition[item] + '%');
		} else if (item == 'dictType') {
			// placeholders.push(`dict_type LIKE ?`);
			// tempParams.push('%' + condition[item] + '%');
			placeholders.push(`dict_type = ?`);
			tempParams.push(condition[item]);
		} else if (item == 'status') {
			placeholders.push(`status = ?`);
			tempParams.push(condition[item]);
		}
	});

	return {
		placeholders: placeholders.join(' AND '),
		tempParams: tempParams,
	};
}

function selectData(condition) {
	const newPlaceholders = getPlaceholders(condition).placeholders;
	if (newPlaceholders) {
		const tempParams = getPlaceholders(condition).tempParams;
		return {
			query: `SELECT * FROM sys_dict_data WHERE ${newPlaceholders} AND delete_time IS NULL `,
			params: [...tempParams],
		};
	} else {
		return {
			query: `SELECT * FROM sys_dict_data WHERE delete_time IS NULL`,
			params: [],
		};
	}
}

function main() {
	// 验证用户权限
	'use api/user/acl/excerpt.js'

	const data = payload.get().query;
	// 格式化这个queryS参数
	let newData = {};
	Object.keys(data).forEach((item) => {
		newData[item] = data[item][0];
	});
	// 分页数据
	const LIST_DATA = selectData(newData);
	const result = SQL.query('system_sql', LIST_DATA.query, ...LIST_DATA.params);
	// 格式化数据
	const newResult = result.map((itemO) => {
		const newObj = {};
		Object.keys(itemO).forEach((item) => {
			newObj[underscoreToCamel(item)] = itemO[item];
		});
		return newObj;
	});

	return okMsg(newResult);
}
